package com.pybox.Schedule;

import com.pybox.entity.TCabinets;
import com.pybox.entity.TCanbinetEventLogs;
import com.pybox.entity.TObjects;
import com.pybox.entity.TObjectsCabinet;
import com.pybox.repository.TCabinetsRepository;
import com.pybox.repository.TCanbinetEventLogsRepository;
import com.pybox.repository.TObjectsCabinetRepository;
import com.pybox.repository.TObjectsRepository;
import com.pybox.util.TimeDealUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by wuyingkai on 2018/10/18.
 */
@Component
@Configurable
@EnableScheduling
@Slf4j
public class ScheduledTasks{

    @Autowired
    private TObjectsRepository tObjectsRepository;
    @Autowired
    private TObjectsCabinetRepository tObjectsCabinetRepository;
    @Autowired
    private TCanbinetEventLogsRepository tCanbinetEventLogsRepository;
    @Autowired
    private TCabinetsRepository tCabinetsRepository;

    @Scheduled(fixedRate = 1000 * 30)
    public void checkCurrentObject(){
        // TODO 暂时注释掉
        //0 是掉线
//        List<TObjects> objectsList = tObjectsRepository.findAllByStatus(0);
//        TCanbinetEventLogs tCanbinetEventLogs = new TCanbinetEventLogs();
//        if (objectsList != null && objectsList.size() >0){
//            for(TObjects ob:objectsList){
//                tCanbinetEventLogs.setCtime(new Timestamp(System.currentTimeMillis()));
//                tCanbinetEventLogs.setEvent("110");//待定
//                tCanbinetEventLogs.setLevel(2);
//                tCanbinetEventLogs.setObjectId(ob.getId());
//                tCanbinetEventLogs.setPos(ob.getPos());
//                tCanbinetEventLogs.setRow(ob.getRow());
//                tCanbinetEventLogs.setState(0);//未确认
//                tCanbinetEventLogs.setUuid(ob.getUuid());
//                tCanbinetEventLogs.setCol(ob.getColumn());
//                tCanbinetEventLogsRepository.save(tCanbinetEventLogs);
//                ob.setIsAddEventLog(1);//本次已经新增到警告，更新下次不再插入
//                tObjectsRepository.save(ob);
//            }
//        }


    }
    //每1分钟执行一次  0 */1 *  * * *
    @Scheduled(cron = "0 */1 *  * * *")
    @Transactional
    public void checkCurrentObjectByCron(){
        //查询所有柜子
         List<TCabinets> list = tCabinetsRepository.findAll();
        //判断物品柜子的状态  0 离线  1 在线
        //得到系统当前时间
       // log.info("nihao0000000000");
        Timestamp timestamp = new Timestamp(System.currentTimeMillis());

        if(list.size()>0){
            for(TCabinets obj :list) {
                //得到柜子的当前状态
                int isOnlines = TimeDealUtils.isOnline(obj.getLastUpdate(), obj.getHeartbeatInvl());
                //判断柜子当前时间单状态，如果不一样才更新柜子状态
                if(obj.getIsOnline() != isOnlines){
                    //更新柜子的状态
                    tCabinetsRepository.updateIsOnline(isOnlines,obj.getId());
                    //写日志
                    if(isOnlines == 0){
                        //写日志
                        TCanbinetEventLogs log = new TCanbinetEventLogs();
                        log.setCabinetId(obj.getId());
                        log.setCtime(timestamp);
                        log.setCustomerId(obj.getCustomerId());
                        log.setUuid(obj.getUuid());
                        log.setEvent("112");
                        tCanbinetEventLogsRepository.save(log);
                    }
                    if(isOnlines == 1){
                        //写日志
                        TCanbinetEventLogs log = new TCanbinetEventLogs();
                        log.setCabinetId(obj.getId());
                        log.setCtime(timestamp);
                        log.setCustomerId(obj.getCustomerId());
                        log.setUuid(obj.getUuid());
                        log.setEvent("113");
                        tCanbinetEventLogsRepository.save(log);
                    }
                }
            }
        }

    }

}